package hos.http;

import android.app.Application;
import android.content.Context;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * <p>Title: Http </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023-02-23 15:44
 */
public class Http {

    public static HttpConfig init(Application context, String host, HttpCall.Factory factory) {
        return config().init(context, host, factory);
    }

    public static HttpConfig config() {
        return HttpConfig.config();
    }

    public static Context getContext() {
        return HttpConfig.config().getContext();
    }

    public static HttpBaseRequest<?> request() {
        return config().getRequest();
    }

    public static HttpBaseRequestBody<?> requestBody() {
        return config().getRequestBody();
    }

    public static HttpBaseRequestSoap<?> requestSoap() {
        return config().getRequestSoap();
    }

    public static HttpBaseRequestDownload<?> requestDownload() {
        return config().getRequestDownload();
    }

    public static HttpBaseRequest<?> get(String url) {
        return request().url(url).method(HttpMethod.GET);
    }

    public static HttpBaseRequestBody<?> post(String url) {
        return requestBody().url(url).method(HttpMethod.POST);
    }

    public static HttpBaseRequestBody<?> upload(String url) {
        return requestBody().url(url).method(HttpMethod.POST);
    }

    public static HttpBaseRequestDownload<?> download(String url) {
        return requestDownload().url(url).method(HttpMethod.DOWNLOAD);
    }

    public static HttpBaseRequestBody<?> put(String url) {
        return requestBody().url(url).method(HttpMethod.PUT);
    }

    public static HttpBaseRequestBody<?> delete(String url) {
        return requestBody().url(url).method(HttpMethod.DELETE);
    }

    public static HttpBaseRequestSoap<?> soap(String url, String methodName, String nameSpace) {
        return requestSoap().init(methodName, nameSpace).url(url).method(HttpMethod.POST);
    }

    public static HttpBaseRequestSoap<?> soap(String url, String methodName) {
        return requestSoap().init(methodName).url(url).method(HttpMethod.POST);
    }

    public static HttpBaseRequestSoap<?> soap(String methodName) {
        return requestSoap().init(methodName).method(HttpMethod.POST);
    }

    public static HttpBaseRequestSoap<?> soap() {
        return requestSoap().method(HttpMethod.POST);
    }


    /**
     * @noinspection unchecked
     */
    public static <T> T create(final Class<T> service) {
        return (T)
                Proxy.newProxyInstance(
                        service.getClassLoader(),
                        new Class<?>[]{service},
                        new InvocationHandler() {
                            private final Object[] emptyArgs = new Object[0];

                            @Override
                            public Object invoke(Object proxy, Method method, Object[] args)
                                    throws Throwable {
                                // If the method is a method from Object then defer to normal invocation.
                                if (method.getDeclaringClass() == Object.class) {
                                    return method.invoke(this, args);
                                }
                                args = args != null ? args : emptyArgs;
                                return config().loadServiceMethod(service, method).invoke(proxy, args);
                            }
                        });
    }
}
